package hadoop.mr09_JOIN;

import hadoop.unit.GlobalConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


public class JoinDriver extends Configured implements Tool {
    private static Configuration conf = GlobalConfiguration.getCon();

    @Override
    public int run(String[] args) throws Exception {

        Job job = Job.getInstance(conf, "stuTask");

        job.setJarByClass(JoinDriver.class);
        job.setMapperClass(JoinMapper.class);
        job.setReducerClass(JoinReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        Path in_path = new Path("hdfs://master:1920/hadoop/Test/RestaurantDemo/demo3/");
        FileInputFormat.setInputPaths(job, in_path);
        Path out_path = new Path("hdfs://master:1920/hadoop/Test/RestaurantDemo/demo6/JoinResult");
        FileOutputFormat.setOutputPath(job, out_path);

        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(out_path)){
            fs.delete(out_path,true);
        }
        return job.waitForCompletion(true)?0:2;
    }
    public static void main(String[] args) throws Exception {
        int run = ToolRunner.run(conf, new JoinDriver(), args);
        System.exit(run);
    }
}

